package be.kuleuven.mume.hulpklassen;

import java.io.IOException;
import java.net.SocketTimeoutException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import be.kuleuven.mume.framework.Mobilenvi;

import android.accounts.AccountsException;
import android.content.Context;
import android.net.http.AndroidHttpClient;
import android.util.Log;

public class HttpHelper {
	
	private static final String TAG = "HttpHelper";
	private static final boolean login = true;
	private static final boolean debug = true;
	
	public static String doGet(String uri, Context context) {

	    AndroidHttpClient client = AndroidHttpClient.newInstance(
	            "IntegrationTestAgent", context);
	        try {
				if(debug)Log.v(TAG, "Requesting:" + uri);
				String baseUri = Mobilenvi.getSingelton().BASEURL;
				HttpGet get = new HttpGet(uri);
				HttpResponse response;
				
				if(login){
					HttpContext httpContext = AuthenticatedAppEngineContext
					      .newInstance(context, baseUri);
					response = client.execute(get, httpContext);
				} else
					response = client.execute(get);
				
				String jString = EntityUtils.toString(response.getEntity());
				return jString;
	        } catch (AccountsException e){
	        	Log.e(TAG,"Exception", e);
	        } catch (SocketTimeoutException e) {
	        	Log.e(TAG, "Exception", e);
	        } catch (Exception e) {
				Log.e(TAG,"Exception", e);
			} 
	        finally {
	          client.close();
	        }
	        return "";
	}
	
	public static void doGetInThread(final String uri, final Context context, final JSONHandler handler){
		try {
			Thread t = new Thread(){
				@Override
				public void run(){
					String result = doGet(uri, context);
					
					JSONObject jsonRes;
					try {
						jsonRes = new JSONObject(result);
						handler.sendJSON(jsonRes);
					} catch (JSONException e) {
						Log.e(TAG, "JSONException for string:" + result, e);
					}
				}
			};
			t.start();
        } catch (Exception e) {
			Log.e(TAG,"Exception", e);
		} 
	}
	
	public static void doPostInThread(final String url, final JSONObject obj, final Context context,
			final JSONHandler handler){
		
		try {
			Thread t = new Thread(){
				@Override
				public void run(){
					String result = doPost(url, obj, context);
					
					JSONObject jsonRes;
					try {
						jsonRes = new JSONObject(result);
						handler.sendJSON(jsonRes);
					} catch (JSONException e) {
						Log.e(TAG, "JSONException for string:" + result, e);
					}
				}
			};
			t.start();
        } catch (Exception e) {
			Log.e(TAG,"Exception", e);
		} 
	}

	public static String doPost(final String url, final JSONObject obj,
			final Context context) {
		final AndroidHttpClient client = AndroidHttpClient.newInstance(
	            "IntegrationTestAgent", context);
		
		String baseUri = Mobilenvi.getSingelton().BASEURL;
		if(debug)Log.v(TAG, "PostingUrl" + url);
		HttpContext httpContext;
		try {
			httpContext = AuthenticatedAppEngineContext
			.newInstance(context, baseUri);
			HttpPost post = new HttpPost(url);
			HttpEntity entity = new StringEntity(obj.toString());
			post.setEntity(entity);
			post.setHeader("Accept", "application/json");
			post.setHeader("Content-type", "application/json");
			HttpResponse response;
			
			if(login)
				response = client.execute(post, httpContext);
			else
				response = client.execute(post);
			
			return EntityUtils.toString(response.getEntity());

		} catch (AuthenticationException e) {
			e.printStackTrace();
		} catch (AccountsException e) {
			e.printStackTrace();
		} catch (IOException e){
			e.printStackTrace();
		} 
		finally {
		    client.close();
		}
		
		return "";
	}
	
	
}
